package mystudy

import (
	"encoding/json"
	"fmt"
)

type User struct {
	Name  string `json:"name,omitempty"`
	Email string `json:"email"`
}

func DemoStructTag() {
	// 序列化
	user := User{Name: "", Email: "john@example.com"}
	data, err := json.Marshal(user)
	if err != nil {
		fmt.Println("序列化失败:", err)
		return
	}
	fmt.Println("序列化结果:", string(data))

	// 反序列化
	var newUser User
	err = json.Unmarshal(data, &newUser)
	if err != nil {
		fmt.Println("反序列化失败:", err)
		return
	}
	fmt.Println("反序列化结果:", newUser)
}